
<?php
session_start();
class Manager extends CI_Controller{
    //put your code here
    
    public function __construct(){
        parent::__construct();
        $this->load->helper("url");
        $this->load->model('Login_m');
        $this->load->model('Account_m');
        $this->load->model('Download_m');
        $this->load->model('Payment_m');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->library("my_layout"); // Sử dụng thư viện layout
        $this->my_layout->setLayout("layout/manager_l"); // load file layout chính (view/layout/frontend.php)
    
    }
    // Controller mặc định cho trang chủ
    public function index(){
        $data['title'] = "ホームページ";
        $data['data'] = "ホームページのデータ";
        $session_data_sm = $this->session->userdata('logged_in_mg');
	$data['id_mg'] = $session_data_sm['id_mg'];
        if($session_data_sm['id_mg']!="")
        {
        $this->my_layout->view("frontend/home",$data);
        }
        else
        {
            redirect('login','refresh');
        }
    }

    public function forum(){
        $data['title'] = "フォーラム";
        $session_data_sm = $this->session->userdata('logged_in_mg');
        if($session_data_sm['id_mg']!="")
        {
	$data['id_mg'] = $session_data_sm['id_mg'];
        $this->my_layout->view("frontend/forum",$data);
        }
        else
        {
            redirect('login','refresh');
        }
    }
    public function about(){
        $data['title'] = "紹介";
        $session_data_sm = $this->session->userdata('logged_in_mg');
        if($session_data_sm['id_mg']!="")
        {
	$data['id_mg'] = $session_data_sm['id_mg'];
        $this->my_layout->view("frontend/about",$data);
        }
        else
        {
            redirect('login','refresh');
        }
    }
    public function add_examiner(){
        $session_data_sm = $this->session->userdata('logged_in_mg');
        if($session_data_sm['id_mg']!="")
        {
	$data['id_mg'] = $session_data_sm['id_mg'];
        $data['title'] = "採点者を追加";
        $this->my_layout->view("add_account/add_examiner_v",$data);
        }
        else
        {
            redirect('login','refresh');
        }
    }
    public function add_maker(){
        $session_data_sm = $this->session->userdata('logged_in_mg');
        if($session_data_sm['id_mg']!="")
        {
	$data['id_mg'] = $session_data_sm['id_mg'];
        $data['title'] = "出題者を追加";
        $this->my_layout->view("add_account/add_maker_v",$data);
        }
        else
        {
            redirect('login','refresh');
        }
    }
    //manager＿profile
    public function manager_profile()
    {
         $data['title'] = "プロファイル";
         $session_data_sm = $this->session->userdata('logged_in_mg');
         if($session_data_sm['id_mg']!="")
         {
	 $data['id_mg'] = $session_data_sm['id_mg'];
         $query=$this->Account_m->get_manager($data['id_mg']);
         foreach ($query as $row)
         {
            $data['id_mg']=$row->id_mg;
            $data['email_mg']=$row->email;
            $data['name_mg']=$row->name;
            $data['phone_mg']=$row->phone;
            $data['address_mg']=$row->address;
          
            $data['year']=$row->year;
         }
         $this->my_layout->view("manager/profile_mg_v",$data);
         }
         else
         {
             redirect('login','refresh');
         }
         
    }
    
    //manager_change pass
    public function manager_change_pass()
    {
        $data['title'] = "パスワードの修正";
        $data['user']="manager";
        $session_data_sm = $this->session->userdata('logged_in_mg');
        if($session_data_sm['id_mg']!="")
        {
	$data['id_mg'] = $session_data_sm['id_mg'];
        $this->my_layout->view("frontend/change_pass",$data);
        }
        else
        {
            redirect('login','refresh');
        }
        
    }
    //manager_change pass
    public function success_change_pass()
    {
        $data['title'] = "パスワードの修正";
        $data['user']="manager";
        $session_data_sm = $this->session->userdata('logged_in_mg');
        if($session_data_sm['id_mg']!="")
        {
	$data['id_mg'] = $session_data_sm['id_mg'];
        $new_pass1=$this->input->post('new_pass');
        $new_pass2=$this->input->post('new_pass_again');
        $old_pass=$this->input->post('old_pass');
        $query= $this->Account_m->search_manager_pass($data['id_mg']);
        foreach ($query as $row)
         {
            $data['password']=$row->password;        
         }
        if($data['password']!=md5($old_pass))
         {
             $data['error1']="古いパスワードが違う";
             $this->my_layout->view("frontend/change_pass",$data);
         }
        else if($new_pass1!=$new_pass2)
        {
        $data['error']="新しいパスワードと新しいパスワードの重複が違う";
        $this->my_layout->view("frontend/change_pass",$data);
        }
        else
        {
            $this->Account_m->update_manager_pass($data['id_mg'],$new_pass1);
            $this->my_layout->view("frontend/home",$data);
        }
        }
        else
        {
            redirect('login','refresh');
        }
    }
    //maker
    public function manage_maker(){
        $session_data_sm = $this->session->userdata('logged_in_mg');
        if($session_data_sm['id_mg']!="")
        {
	$data['id_mg'] = $session_data_sm['id_mg'];
        $data['title'] = "出題者管理";
        //$data['user'] = "manager";
        $data['query_list_manager']=$this->Account_m->list_users1('maker',$data['id_mg'] );
        $this->my_layout->view("edit_account/update_maker_success_v",$data);
        }
        else
            redirect ('login','refresh');
    }
     public function search_maker(){
        $session_data_sm = $this->session->userdata('logged_in_mg');
        if($session_data_sm['id_mg']!="")
        {
	$data['id_mg'] = $session_data_sm['id_mg'];
        $data['title'] = "マネージャーを変更";
        $search=  $this->input->post('timkiem1'); 
        //echo $search;
        if($this->Account_m->search_database_null_mk($search,'maker')>0){
               // $query['query']=$this->Account_m->search_database($search,'manager');
                $data['query']=$this->Account_m->search_database_mk($search,'maker');
                $this->my_layout->view("edit_account/edit_maker_v",$data);
        }
        else{
            echo "<script type='text/javascript'>";	
	    echo "alert ('結果がありません!!! ');";
	    echo "</script>";
            redirect('manager/manage_maker','refresh');
        }
        }
        else
        {
            redirect('login','refresh');
        }
    }
     public function update_maker($id_mk){
         $session_data_sm = $this->session->userdata('logged_in_mg');
         if($session_data_sm['id_mg']!=""){
         $data['id_mg'] = $session_data_sm['id_mg'];
         $data['title'] = "update info";
         $query=$this->Account_m->get_maker($id_mk);
         //echo $data['id_mg'] ; 
         foreach ($query as $row)
         {
            $data['id_mk']=$row->id_mk;
            $data['email_mk']=$row->email;
            $data['name_mk']=$row->name;
            $data['phone_mk']=$row->phone;
            $data['address_mk']=$row->address;
         }
         $this->my_layout->view("edit_account/update_maker_v",$data);
         }
         else
         {
             redirect('login','refresh');
         }
     }
     public function update_maker_success(){
         $session_data_sm = $this->session->userdata('logged_in_mg');
         if($session_data_sm['id_mg']!="")
         {
         $data['id_mg'] = $session_data_sm['id_mg'];
         $data['title'] = "update success";
         //$password=$this->input->post('password');
         $id_mk=$this->input->post('txtIDManager');
         $name_mk=$this->input->post('txtNameManager');
         //$id_mg=$this->input->post('id');
         $email_mk=$this->input->post('txtEmailManager');
         $address_mk=$this->input->post('txtAddressManager');
         $phone_mk=$this->input->post('txtPhoneManager');
         //echo $id_mk;
         $this->Account_m->update_maker($id_mk,$email_mk,$name_mk,$address_mk,$phone_mk,$session_data_sm['id_mg']);
         $query=$this->Account_m->get_maker($id_mk);
         foreach ($query as $row)
         {
            $data['id_mg']=$row->id_mk;
            $data['email_mg']=$row->email;
            $data['name_mg']=$row->name;
            $data['phone_mg']=$row->phone;
            $data['address_mg']=$row->address;
         }
         $this->my_layout->view("frontend/profile_v",$data);

         //$data['query_list_manager']=$this->Account_m->list_users('maker');
         $this->my_layout->view("edit_account/update_maker_success_v",$data);
         }
         else
             redirect ('login','refresh');
      }
      public function delete_maker($id_mk)
      {
         $session_data_sm = $this->session->userdata('logged_in_mg');
         if($session_data_sm['id_mg']!="")
         {
         $data['id_mg'] = $session_data_sm['id_mg'];
         $data['title'] = "Delete success";
         $this->Account_m->delete_maker($id_mk);
         $data['query_list_manager']=$this->Account_m->list_users1('maker',$data['id_mg'] );
         $this->my_layout->view("edit_account/update_maker_success_v",$data);
         }
         else
             redirect ('login','refresh');
         
      }
// exminer
      
      public function manage_examiner(){
        $session_data_sm = $this->session->userdata('logged_in_mg');
        if($session_data_sm['id_mg']!="")
        {
	$data['id_mg'] = $session_data_sm['id_mg'];
        $data['title'] = "出題者管理";
        //$data['user'] = "manager";
        $this->my_layout->view("edit_account/search_examiner_v",$data);
        }
        else
        {
            redirect('login','refresh');
        }
    }
     public function search_examiner(){
        $session_data_sm = $this->session->userdata('logged_in_mg');
        if($session_data_sm['id_mg']!="")
        {
	$data['id_mg'] = $session_data_sm['id_mg'];
        $data['title'] = "マネ
            ージャーを変更";
        $search=  $this->input->post('timkiem1'); 
       //echo $search;
        if($this->Account_m->search_database_null_ex($search,'examiner')>0){
               // $query['query']=$this->Account_m->search_database($search,'manager');
                $data['query']=$this->Account_m->search_database_ex($search,'examiner');
                $this->my_layout->view("edit_account/edit_examiner_v",$data);
        }
        else{
            echo "<script type='text/javascript'>";	
	    echo "alert ('結果がありません!!! ');";
	    echo "</script>";
            redirect('manager/manage_examiner','refresh');
        }
        }
        else
            redirect ('login','refresh');
    }
     public function update_examiner($id_ex){
         $session_data_sm = $this->session->userdata('logged_in_mg');
         if($session_data_sm['id_mg']!="")
         {
         $data['id_mg'] = $session_data_sm['id_mg'];
         $data['title'] = "update info";
         $query=$this->Account_m->get_examiner($id_ex);
         //echo $data['id_mg'] ; 
         foreach ($query as $row)
         {
            $data['id_ex']=$row->id_ex;
            $data['email_ex']=$row->email;
            $data['name_ex']=$row->name;
            $data['phone_ex']=$row->phone;
            $data['address_ex']=$row->address;
         }
         $this->my_layout->view("edit_account/update_examiner_v",$data);
         }
         else
             redirect ('login','refresh');
     }
     public function update_examiner_success(){
         $session_data_sm = $this->session->userdata('logged_in_mg');
         if($session_data_sm['id_mg']!="")
         {
         $data['id_mg'] = $session_data_sm['id_mg'];
         $data['title'] = "update success";
         //$password=$this->input->post('password');
         $id_ex=$this->input->post('txtIDManager');
         $name_ex=$this->input->post('txtNameManager');
         //$id_mg=$this->input->post('id');
         $email_ex=$this->input->post('txtEmailManager');
         $address_ex=$this->input->post('txtAddressManager');
         $phone_ex=$this->input->post('txtPhoneManager');
         //echo $id_mk;
         $this->Account_m->update_examiner($id_ex,$email_ex,$name_ex,$address_ex,$phone_ex);
         $data['query_list_manager']=$this->Account_m->list_users('examiner');
         $this->my_layout->view("edit_account/update_examiner_success_v",$data);
         }
         else
             redirect ('login','refresh');
      }
      public function delete_examiner($id_ex)
      {
         $session_data_sm = $this->session->userdata('logged_in_mg');
         if($session_data_sm['id_mg']!="")
         {
         $data['id_mg'] = $session_data_sm['id_mg'];
         $data['title'] = "Delete success";
         $this->Account_m->delete_examiner($id_ex);
         $data['query_list_manager']=$this->Account_m->list_users('examiner');
         $this->my_layout->view("edit_account/update_examiner_success_v",$data);
         }
         else
         {
             redirect('login','refresh');
         }
         
      }
    public function download()
    {
        $data['title']="ダウンロード";
        $session_data_sm = $this->session->userdata('logged_in_mg');
        if($session_data_sm['id_mg']!="")
        {
        $data['id_mg'] = $session_data_sm['id_mg'];
        $data['user']="manager";
        $this->load->helper(array('form'));
        $this->my_layout->view('download/download_v',$data); 
        }
        else
            redirect ('login','refresh');
    }
    function verifydownload()
    {
        $data['title']="ダウンロード";
        $session_data_sm = $this->session->userdata('logged_in_mg');
        if($session_data_sm['id_mg']!="")
        {
        $data['id_mg'] = $session_data_sm['id_mg'];
        $filedown=$this->input->post('filedown');
        $name=$this->Download_m->getCSVfromDB($filedown);
        $data['name']=$name;
        foreach ($name as $rows) {
        ////code hieu
            $session_array_download=array('name'=>$rows->name,'link'=>$rows->link);
            $this->session->set_userdata('download_file',$session_array_download);
            //echo $rows->Name;


           //echo anchor('downfile', 'Download');
            //anchor($uri, $title, $attributes)
            //echo "<br>";         
        }
        $this->my_layout->view('download/downfile_v',$data);
        }
        else
            redirect ('login','refresh');
    }
    public function downfile($id)
    {
        //$session_download = $this->session->userdata('download_file');
        //$test=$session_download['name'];
        //$test=$_GET['STT'];
        $data = file_get_contents("./csv_source/".$id); 
        force_download($id, $data);
    }
    //// Code Payment Manager
    public function payment()
    {
        $data['title']="契約期間";
        $session_data_mg = $this->session->userdata('logged_in_mg');
        $id_mg = $session_data_mg['id_mg'];
        $result=$this->Payment_m->show_payment_by_id_mg($id_mg);
        if ($result==NULL)
        {
            $data['error']="あなたの大学はまだ試験を行いません。";
            echo "<script type='text/javascript'>";	
            echo "alert ('契約情報はありません、すぐ管理者と連絡します!!! ');";
            echo "</script>";
            $this->index();
        }
        else
        {
            $data['query']=$this->Payment_m->show_payment_by_id_mg($id_mg);
            $this->my_layout->view('payment/manager_list_payment_v',$data);
        }
        
    }
    public function show_payment()
    {
        $data['title'] = "契約作成";
        $session_data_mg = $this->session->userdata('logged_in_mg');
        $id_mg = $session_data_mg['id_mg'];
        $time_choice=$this->input->post('time');
        $data['query']=$this->Payment_m->create_CSV_payment_by_month($id_mg,$time_choice);
        $data['message']="";
        $this->my_layout->view('payment/success_create_payment_v',$data); 
    }
    //
    public function logout()
    {
        $session_data_mg = $this->session->userdata('logged_in_mg');
        $id_mg = $session_data_mg['id_mg'];
        $this->Login_m->update_online_mg($id_mg,0);
        $this->session->unset_userdata('logged_in_mg');
        session_destroy();
        redirect('login','refresh');
    }
    
    
}
?>
